<template>
  <div>
    <el-card>
      <!-- 头部按钮 -->
      <template #header>
        <el-button
          type="primary"
          size="small"
          icon="el-icon-plus"
          @click="isShowDialog"
          v-has="`btn.Trademark.add`"
          >添加</el-button
        >
      </template>
      <!-- 表格区 -->
      <el-table :data="treadMarkList" border style="width: 100%">
        <!-- 序号 -->
        <el-table-column
          prop="date"
          label="序号"
          width="180"
          align="center"
          type="index"
        >
        </el-table-column>
        <!-- 品牌名称 -->
        <el-table-column
          prop="tmName"
          label="品牌名称"
          width="180"
          align="center"
        >
        </el-table-column>
        <!-- 品牌logo -->
        <el-table-column label="品牌logo" align="center">
          <template v-slot="{ row }">
            <img :src="row.logoUrl" alt="" style="width: 100px" />
          </template>
        </el-table-column>
        <!-- 品牌操作 -->
        <el-table-column prop="address" label="品牌操作" align="center">
          <template v-slot="{ row }">
            <!-- 编辑按钮 -->
            <el-button
              type="primary"
              size="small"
              icon="el-icon-edit"
              @click="toEdit(row)"
              v-has="'btn.Trademark.update'"
            ></el-button>
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              size="small"
              icon="el-icon-delete"
              @click="removeTreadMark(row)"
              v-has="'btn.Trademark.remove'"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        style="margin-top: 20px"
        :current-page="pageNo"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout=" prev, pager, next, jumper, -> ,sizes, total"
        :total="total"
        @current-change="changePageNo"
        @size-change="changePageSize"
      >
      </el-pagination>
    </el-card>

    <!-- 添加品牌区域 -->
    <el-dialog title="添加品牌" :visible.sync="dialogFormVisible" width="40%">
      <el-form
        :model="dialogForm"
        style="widtn: 80%"
        :rules="rules"
        ref="dialogForm"
      >
        <el-form-item label="品牌名称" label-width="100px" prop="tmName">
          <el-input v-model="dialogForm.tmName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="品牌LOGO" label-width="100px" prop="logoUrl">
          <el-upload
            class="avatar-uploader"
            action="/app/admin/product/fileUpload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <img
              v-if="dialogForm.logoUrl"
              :src="dialogForm.logoUrl"
              class="avatar"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 引入接口
import {
  asyncGetTreadMarkList,
  asyncAddTreadMark,
  asyncUpDataTreadMark,
  asyncDeleteTreadMark,
} from "@/api/treadmark/treadmark";
// 引入element-ui
import { Message } from "element-ui";
// 引入深拷贝函数
import cloneDeep from "lodash/cloneDeep";
export default {
  name: "Trademark",
  // 数据
  data() {
    return {
      // 品牌管理的列表
      treadMarkList: [],
      // 总条数
      total: 0,
      // 一页显示几条
      pageSize: 5,
      // 当前第几页
      pageNo: 1,
      /* 对话框的数据 */
      dialogTableVisible: false,
      dialogFormVisible: false,
      dialogForm: {
        id: "",
        tmName: "",
        logoUrl: "",
      },
      // 表单校验
      rules: {
        tmName: [
          { required: true, message: "请输入品牌名称", trigger: "blur" },
        ],
        logoUrl: [{ required: true, message: "请上传图片", trigger: "blur" }],
      },
    };
  },
  // 方法
  methods: {
    // 请求品牌的接口
    async getTreadMarkList() {
      try {
        let res = await asyncGetTreadMarkList(this.pageNo, this.pageSize);
        // console.log(res);
        this.total = res.data.total;
        this.treadMarkList = res.data.records;
      } catch (error) {
        console.log(error);
      }
    },
    // 分页器改变页数的回调
    changePageNo(page) {
      this.pageNo = page;
      this.getTreadMarkList();
    },
    // 改变一页显示个数的回调
    changePageSize(page) {
      this.pageSize = page;
      this.pageNo = 1;
      this.getTreadMarkList();
    },

    /* 添加品牌对话框的回调 */
    // 控制对话框的显示与隐藏的回调
    isShowDialog() {
      // this.dialogForm = {};
      this.dialogFormVisible = true;
      this.dialogForm.tmName = "";
      this.dialogForm.logoUrl = "";
    },
    // 上传文件的回调
    handleAvatarSuccess(res) {
      // console.log(res);
      this.dialogForm.logoUrl = res.data;
    },
    // 点击保存的回调
    async save() {
      this.$refs.dialogForm.validate(async (vaild) => {
        // console.log(vaild, "valid");
        if (vaild) {
          try {
            // console.log(this.dialogForm.id, "id");
            if (this.dialogForm.id) {
              await asyncUpDataTreadMark(this.dialogForm);
            } else {
              await asyncAddTreadMark(this.dialogForm);
            }
            this.getTreadMarkList();
            this.dialogFormVisible = false;
            Message.success(this.dialogForm.id ? "修改成功" : "添加成功");
          } catch (error) {
            Message.error(this.dialogForm.id ? "修改失败" : "添加失败");
            console.log(error);
          }
        } else {
          return false;
        }
      });
    },
    // 点击编辑按钮的回调
    toEdit(row) {
      // console.log(row);
      this.dialogForm = cloneDeep(row);
      this.dialogFormVisible = true;
    },
    // 点击删除的回调
    async removeTreadMark(row) {
      this.$confirm(`是否删除${row.tmName}?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          try {
            await asyncDeleteTreadMark(row.id);
            this.getTreadMarkList();
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          } catch (error) {}
        })
        .catch(() => {
          this.$message({
            type: "warning",
            message: "取消删除",
          });
        });
    },
  },
  // 生命周期
  mounted() {
    this.getTreadMarkList();
  },
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>